package com.manjiwang.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.manjiwang.order.entity.Orders;
import com.manjiwang.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单控制层
 *
 * @Author Yang Hai Feng
 * @Date 2021/8/10 20:17
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/getOrder")
    public JSONObject getOrder(@RequestParam("id") String id) {
        Orders order = orderService.getById(id);
        return JSONObject.parseObject(JSONObject.toJSONString(order));
    }

    /**
     * 模拟订单增加购买商品数量，每次+1
     *
     * @param id
     * @return
     */
    @GetMapping("/updateOrder")
    public JSONObject updateOrder(@RequestParam("id") String id) {
        Orders order = orderService.getById(id);
        order.setBuyCount(order.getBuyCount() + 1);
        orderService.updateById(order);
        return JSONObject.parseObject(JSONObject.toJSONString(order));
    }

}
